<template>
  <el-form label-width="100px">
    <el-form-item label="sku名称">
      <el-input placeholder="sku名称" v-model="skuData.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格（元）">
      <el-input
        placeholder="价格（元）"
        type="number"
        v-model="skuData.price"
      ></el-input>
    </el-form-item>
    <el-form-item label="重量（g）">
      <el-input
        placeholder="重量（g）"
        type="number"
        v-model="skuData.weight"
      ></el-input>
    </el-form-item>
    <el-form-item label="sku描述">
      <el-input
        type="textarea"
        placeholder="sku描述"
        v-model="skuData.skuDesc"
      ></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form inline="true" label-width="40px">
        <el-form-item
          v-for="(item, index) in attrArr"
          :key="index"
          :label="item.attrName"
        >
          <el-select v-model="item.attrIdAndValueId">
            <el-option
              :value="`${item.id}:${attrValue.id}`"
              v-for="attrValue in item.attrValueList"
              :key="attrValue.id"
              :label="attrValue.valueName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="a">
        <el-form-item
          v-for="(item, index) in saleArr"
          :key="index"
          :label="item.saleAttrName"
        >
          <el-select v-model="item.saleAttrIdAndSaleAttrValueId">
            <el-option
              :value="`${item.id}:${saleArrValue.id}`"
              v-for="saleArrValue in item.spuSaleAttrValueList"
              :key="saleArrValue.id"
              :label="saleArrValue.saleAttrValueName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="table">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="图片">
          <template #default="{ row }">
            <img :src="row.imgUrl" height="100" />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" @click="setImg(row)">设置默认</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="Cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>
<script setup lang="ts">
import { reqAttr } from '@/api/product/attr/attr'
import {
  reqSpuSaleAttrList,
  reqSpuImageList,
  reqAddSku,
} from '@/api/product/spu/spu'
import type { SkuData, SpuData } from '@/api/product/spu/type'
import { ElMessage } from 'element-plus'
import { reactive, ref } from 'vue'
let a = true
let emit = defineEmits(['changeScene'])
//取消方法
const Cancel = () => {
  emit('changeScene', 0)
  skuData.category3Id = ''
  skuData.spuId = ''
  skuData.tmId = ''
  skuData.skuName = ''
  skuData.price = ''
  skuData.weight = ''
  skuData.skuDesc = ''
  skuData.skuDefaultImg = ''
}
//tabl数据
const table = ref<any>([])

//sku数据
let skuData = reactive<SkuData>({
  category3Id: '',
  spuId: '',
  tmId: '',
  skuName: '',
  price: '',
  weight: '',
  skuDesc: '',
  skuDefaultImg: '',
})
//平台属性
let attrArr = ref<any>([])
//销售属性
let saleArr = ref<any>([])
//照片链接
let imgArr = ref<any>([])
//当前子组件的方法暴露
const initSkuData = async (
  c1Id: number | string,
  c2Id: number,
  row: SpuData,
) => {
  skuData.category3Id = row.category3Id
  skuData.spuId = row.id as number
  skuData.tmId = row.tmId
  let [req1, req2, req3] = await Promise.all([
    reqAttr(c1Id, c2Id, row.category3Id),
    reqSpuSaleAttrList(row.id as number),
    reqSpuImageList(row.id as number),
  ])
  attrArr.value = req1.data
  saleArr.value = req2.data
  imgArr.value = req3.data
}
//设置默认
const setImg = (row: any) => {
  imgArr.value.forEach((element: any) => {
    table.value.toggleRowSelection(element, false)
  })
  table.value.toggleRowSelection(row, true)
  skuData.skuDefaultImg = row.imgUrl
}
//保存
const save = async () => {
  //整理属性
  //平台属性
  skuData.skuAttrValueList = attrArr.value.reduce((pre: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':')
      pre.push({
        attrId,
        valueId,
      })
    }
    return pre
  }, [])
  //销售属性
  skuData.skuSaleAttrValueList = saleArr.value.reduce((pre: any, next: any) => {
    if (next.saleAttrIdAndSaleAttrValueId) {
      let [saleAttrId, saleAttrValueId] =
        next.saleAttrIdAndSaleAttrValueId.split(':')
      pre.push({
        saleAttrId,
        saleAttrValueId,
      })
    }
    return pre
  }, [])
  //发请求
  let req = await reqAddSku(skuData)
  if (req.code === 200) {
    ElMessage({
      type: 'success',
      message: '保存成功',
    })
    Cancel()
  } else {
    ElMessage({
      type: 'error',
      message: '保存失败',
    })
  }
}
defineExpose({
  initSkuData,
})
</script>
<style scoped lang="scss"></style>
